package com.xored.glance.internal.ui.search;

import org.eclipse.core.runtime.IProgressMonitor;

import com.xored.glance.ui.panels.ISearchPanel;

public class SearchProgressMonitor implements IProgressMonitor {

	private ISearchPanel panel;
	private double total = 1;
	private double current;
	private boolean cancel;

	public SearchProgressMonitor(ISearchPanel panel) {
		this.panel = panel;
	}

	@Override
	public void beginTask(String name, int totalWork) {
		total = totalWork;
	}

	@Override
	public void done() {
		if (isCanceled()) {
			return;
		}
		panel.indexing(-1);
	}

	@Override
	public void internalWorked(double work) {
		if (isCanceled()) {
			return;
		}
		current += work / total;
		if (current > 1) {
			current = 1;
		}
		panel.indexing(current);
	}

	@Override
	public boolean isCanceled() {
		return cancel || panel.getControl().isDisposed();
	}

	@Override
	public void setCanceled(boolean value) {
		this.cancel = value;
	}

	@Override
	public void setTaskName(String name) {
	}

	@Override
	public void subTask(String name) {
	}

	@Override
	public void worked(int work) {
		internalWorked(work);
	}

}